package com.lgj.springboot_lgj.web.controller;

import com.lgj.springboot_lgj.aop.Logging;
import com.lgj.springboot_lgj.bean.basic.BaseUser;
import com.lgj.springboot_lgj.service.IBaseUserService;
import com.lgj.springboot_lgj.utils.EntityUtil;
import com.lgj.springboot_lgj.utils.Result;
import com.lgj.springboot_lgj.utils.ResultUtil;
import com.lgj.springboot_lgj.web.vm.UserInsertVM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.tomcat.jni.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * lll
 * @author LDD
 * @date 2022/6/17 20:25
 */
@Api(tags = "登录模块")
@RestController
public class BaseUserController {
    @Autowired//自动装配ioc容器的接口类型的对象
    private IBaseUserService service;

    @ApiOperation("登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",required = true),
            @ApiImplicitParam(name = "password",value = "密码",required = true)
    })
    @PostMapping("/login")
    public Result login(String username, String password){
        String token = service.login(username, password);
        return ResultUtil.success(token);
    }
    @Logging
    @GetMapping("/auth/identity")
    @ApiOperation("认证测试")
    public Result Identity(){
        //当用户访问/auth/路径下的接口必须提供token字符串进行身份认证
        return ResultUtil.success("访问成功");
    }
    @ApiOperation("注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",required = true),
            @ApiImplicitParam(name = "password",value = "密码",required = true),
            @ApiImplicitParam(name = "telephone",value = "电话",required = true),
            @ApiImplicitParam(name = "realname",value = "真实名称",required = true),
            @ApiImplicitParam(name = "icon",value = "头像url",required = true),
            @ApiImplicitParam(name="gender",value ="性别0女 1男",required = true,dataType = "int"),
            @ApiImplicitParam(name="dob",value = "出生日期",required = true),
            @ApiImplicitParam(name="email",value = "邮箱",required = true),
    })
    @PostMapping("/register")
    public Result register(UserInsertVM vm){
        //1.接收前端传递参数
        //2.调用service方法
        //3.将程序处理的结果响应给前端
        service.addUser(EntityUtil.vm2Entity(vm,BaseUser.class));
        //方法执行成功，表示返回操作成功的提示信息
        return ResultUtil.success("注册成功");
    }

    @ApiOperation("用户退出")
    @PostMapping("/logout")
        public Result logout() {
            return ResultUtil.success("退出成功");
        }
}
